package cn.xyf.mzmy.server.utils;

import com.alibaba.dashscope.assistants.Assistant;
import dev.langchain4j.community.model.dashscope.QwenChatModel;
import dev.langchain4j.data.message.*;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;

import dev.langchain4j.service.AiServices;
import dev.langchain4j.service.MemoryId;

public class AiChatUtil {

    private static String apiKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

    // AI服务
    interface Assistant2 {
        String chat(@MemoryId int memoryId, @dev.langchain4j.service.UserMessage String userMessage);
    }

    // 为每个用户创建单独的聊天记录
    public static void createChatMemoryForEachUser(){
        // 创建模型
        ChatModel qwenModel = QwenChatModel.builder()
                .apiKey(apiKey)
                .modelName("qwen-plus")
                .build();

        Assistant2 assistant = AiServices.builder(Assistant2.class)
                .chatModel(qwenModel)
                .chatMemoryProvider(memoryId -> MessageWindowChatMemory.withMaxMessages(10))
                .build();

        System.out.println(assistant.chat(1, "Hello, my name is Klaus"));
        // Hi Klaus! How can I assist you today?

        System.out.println(assistant.chat(2, "Hello, my name is Francine"));
        // Hello Francine! How can I assist you today?

        System.out.println(assistant.chat(1, "What is my name?"));
        // Your name is Klaus.

        System.out.println(assistant.chat(2, "What is my name?"));
        // Your name is Francine.

    }

    interface Assistant1 {
        String chat(String message);
    }

    // 聊天记忆
    public static void chatMemoryDemo(){

        ChatMemory chatMemory = MessageWindowChatMemory.withMaxMessages(10);

        ChatModel qwenModel = QwenChatModel.builder()
                .apiKey(apiKey)
                .modelName("qwen-plus")
                .build();

        Assistant1 assistant = AiServices.builder(Assistant1.class)
                .chatModel(qwenModel)
                .chatMemory(chatMemory)
                .build();

        String answer = assistant.chat("Hello! My name is Klaus.");
        System.out.println(answer); // Hello Klaus! How can I assist you today?

        String answerWithName = assistant.chat("What is my name?");
        System.out.println(answerWithName); // Your name is Klaus.
    }

    // ChatModel 和 ChatMessage
    public static void demo(){
        // ChatModel
        ChatModel qwenModel = QwenChatModel.builder()
                .apiKey(apiKey)
                .modelName("qwen-vl-plus")
                .build();
        // ChatMessage
        SystemMessage systemMessage = SystemMessage.from("你是一个图片描述大师");
        UserMessage userMessage = UserMessage.from(
                // Content
                TextContent.from("描述一下这张图片"),
                ImageContent.from("https://my-picture-1312810698.cos.ap-shanghai.myqcloud.com/picture/1749977749151.jpg")
        );

        qwenModel.chat(systemMessage,userMessage);
        ChatResponse response = qwenModel.chat(userMessage);
        System.out.println(response.aiMessage().text());
    }

    public static void main(String[] args) {

    }

}
